package com.zmn.uac.api.controllers.oauth.bind;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.uac.api.constant.HeaderParamKeyConst;
import com.zmn.uac.api.controllers.BaseController;
import com.zmn.uac.client.UserBindClient;
import com.zmn.uac.common.info.mobile.MobileCaptchaUserLoginInfo;
import com.zmn.uuc.common.constant.CaptchaConsts;
import com.zmn.uuc.common.dio.user.UserBindMobileNoCaptchaByUserIdDIO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Optional;

/**
 * 类描述：绑定
 *
 * @author xujie
 * @since 2021/06/15 11:24
 */
@Api(tags = "绑定")
@Slf4j
@RestController
@RequestMapping("/bind")
public class BindController extends BaseController {

    private static final String TAG = "绑定";

    @Resource
    UserBindClient userBindClient;

    /**
     * 是否绑定手机号
     *
     * @return ResponseDTO<Boolean>
     * @author xujie
     * @since 2021/06/22
     */
    @ApiOperation(value = "是否绑定手机号")
    @GetMapping(value = "/isBindMobile", headers = {HeaderParamKeyConst.TOKEN_KEY})
    public ResponseDTO<Boolean> isBindMobile(HttpServletRequest request) {

        Long userId = (Long) request.getAttribute(HeaderParamKeyConst.USER_ID);
        Boolean bindStatus = userBindClient.isBindMobileByUserId(userId);

        return new ResponseDTO<>(bindStatus);
    }


    /**
     * 通过验证码绑定手机号
     *
     * @param mobileCaptchaInfo mobileCaptchaInfo
     * @return ResponseDTO<UserAuthLoginDRO>
     * @author xujie
     * @since 2021/06/14
     */
    @ApiOperation(value = "通过验证码绑定手机号")
    @PostMapping(value = "/mobileByCaptcha",produces = MediaType.APPLICATION_JSON_VALUE, headers = {HeaderParamKeyConst.PLAT_ID_KEY, HeaderParamKeyConst.TOKEN_KEY})
    public ResponseDTO<String> mobileByCaptcha(HttpServletRequest request,
                                               @RequestBody @Valid
                                               @NotNull(message = "用户手机号验证码信息不能为null") MobileCaptchaUserLoginInfo mobileCaptchaInfo) {

        Integer plat = Integer.valueOf(Optional.ofNullable(request.getHeader(HeaderParamKeyConst.PLAT_ID_KEY)).orElse("0"));
        Long userId = (Long) request.getAttribute(HeaderParamKeyConst.USER_ID);

        // 校验验证码
        ResponseDTO<Boolean> validateCaptcha = validateCaptcha(plat, mobileCaptchaInfo.getMobile(), mobileCaptchaInfo.getCaptcha());
        if (!validateCaptcha.isSuccess()) {
            validateCaptcha = validateCaptcha(plat, mobileCaptchaInfo.getMobile(), mobileCaptchaInfo.getCaptcha(), CaptchaConsts.CAPTCHA_TYPE_USER_BIND_LOGIN);
            if (!validateCaptcha.isSuccess()) {
                return ResponseDTO.fail(validateCaptcha.getMessage());
            }
        }
        // 绑定手机号
        UserBindMobileNoCaptchaByUserIdDIO bindMobileByUserId = new UserBindMobileNoCaptchaByUserIdDIO();
        bindMobileByUserId.setUserId(userId);
        bindMobileByUserId.setMobile(mobileCaptchaInfo.getMobile());
        userBindClient.bindMobileNoCaptchaByUserId(bindMobileByUserId);
        return ResponseDTO.success(mobileCaptchaInfo.getMobile());
    }
}
